//
//  TestView.m
//  POP_User
//
//  Created by Rowling on 2019/10/24.
//  Copyright © 2019 Rowling. All rights reserved.
//

#import "TestView.h"
#import "TestCell.h"
#import "TestNetTool.h"
#import "XMIndexHeaderView.h"

#define SG_SCREEN_WIDTH      ([UIScreen mainScreen].bounds.size.width)
#define SG_SCREEN_HEIGHT     ([UIScreen mainScreen].bounds.size.height)
#define SG_IS_IPhoneX_All    ([UIScreen mainScreen].bounds.size.height == 812 || [UIScreen mainScreen].bounds.size.height == 896)
#define SG_BOTTOM_SPACE     (SG_IS_IPhoneX_All ? 34.0 : 0.0)
#define SG_STATUS_HEIGHT     (CGRectGetMaxY([UIApplication sharedApplication].statusBarFrame))

@interface TestView()<UITableViewDelegate,UITableViewDataSource,XMIndexHeaderViewDelegate>
@property(nonatomic,strong)XMIndexHeaderView * indexView;
@property(nonatomic,assign)NSInteger  currentIndex;
@property(nonatomic,strong)NSArray * dataArray;
@property(nonatomic,strong)NSArray * socketDataArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end
@implementation TestView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.estimatedRowHeight = 100;
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.tableFooterView = [UIView new];
    self.indexView = [[XMIndexHeaderView alloc] initWithFrame:CGRectMake(0, SG_STATUS_HEIGHT, SG_SCREEN_WIDTH, 44) WithTitleArray:@[@"接口",@"Socket"] animation:NO];
    [self addSubview:self.indexView];
    self.indexView.delegateXM = self;
//    self.tableView.mj_header = [SGRefresh addNormalRefreshHeaderWithBlock:^{
//        [self refreshDataAndReload];
//    }];
}

-(void)reloadData {
//    if (self.superview) {
//
//    } else {
        [self refreshDataAndReload];
//    }
}

-(void)refreshDataAndReload {
    self.dataArray = [TestNetTool shareInstance].dataArray;
    self.socketDataArray = [TestNetTool shareInstance].socektDataArray;
    [self.tableView reloadData];
//    [self.tableView.mj_header endRefreshing];
}

//-(void)layoutSubviews {
//    [super layoutSubviews];
//    self.indexView.frame = CGRectMake(0, SG_STATUS_HEIGHT, SG_SCREEN_WIDTH, 44);
//}

-(void)XMIndexHeaderViewDidselectedWithIndex:(NSInteger)index {
    self.currentIndex = index;
    [self.tableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.currentIndex == 0 ?  self.dataArray.count : self.socketDataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TestCell * cell = [TestCell cellWithTalbelView:tableView];
    if (self.currentIndex == 0) {
        cell.model = self.dataArray[indexPath.row];
    } else {
        cell.socketModel = self.socketDataArray[indexPath.row];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TestCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString * past = cell.contentLabel.text;
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = past;
//    [SGDialog showWeakStr:@"粘贴成功"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.currentIndex == 0) {
        TestNetToolModel * model = self.dataArray[indexPath.row];
        return  model.height;
    } else {
        return 0;
    }
}

- (IBAction)close:(id)sender {
    [self removeFromSuperview];
}

- (IBAction)clean:(id)sender {
    if (self.currentIndex == 0) {
        [[TestNetTool shareInstance].dataArray removeAllObjects];
        self.dataArray = @[];
    } else {
        [[TestNetTool shareInstance].socektDataArray removeAllObjects];
        self.socketDataArray = @[];
    }
   [self.tableView reloadData];
}

- (IBAction)logClick:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TestToolLogClick" object:nil];
    [self removeFromSuperview];
}


@end
